<?php

    require_once( 'Database.php' );
    require_once( 'File.php' );

    use PHPK\Database;
    use PHPK\File;

    $oDbh = new Database( dirname( dirname( __FILE__ ) ) . '/pdfbldr.db' );
    $oPdo = $oDbh->pdo;
    $oSql = $oPdo->prepare( 'SELECT content FROM pdfbldr WHERE docid = :docid' );

    $arResult = array();

    $strContent = '';
    $strDocID = !empty( $_GET[ 'docid' ] ) ? $_GET[ 'docid' ] : '';
    $strFile = dirname( dirname( __FILE__ ) ) . '/pdf/' . $strDocID . '.pdf';

    $oSql->bindParam( ':docid', $strDocID );
    $arResult[ 'exec' ] = $oSql->execute() ? true : false;

    $oSql->bindColumn( 'content', $strContent );
    $oSql->fetch();
    $arResult[ 'html' ] = $strContent;

    date_default_timezone_set( 'Asia/Calcutta' );

    $oFlh = new File( $strFile, $strContent );
    $arResult[ 'file' ] = 'pdf/' . $strDocID. '.pdf';

    header( 'Content-Type: application/json' );
    print json_encode( $arResult );

?>
